<?php
namespace Home\Controller;
use Common\Controller\CommonController;
class UserController extends HomeController {
 	
	public function _initialize(){
 		parent::_initialize();
	}
	/**
	 * 获取用户信息
	 */
	public function getUser(){
	    $open_id =I('openid');
		$re = M('Member')->where(array('openid' =>$open_id))->find();
		$fin=M('finance')->where(array('open_id'=>$open_id))->select();
		$jifen1=0;
		foreach($fin as $fk=>$fv){
			if($fv['sz_type']==1 && $fv['type']==2){
				$jifen1+=$fv['money'];
			}
		}
		$re['jifen']=$jifen1;
		$this->ajaxReturn($re);
	}
	//积分明细
	public function jifendetail(){
		$openid=I('openid');
		$f=M('rob_log');//查询积分助力记录
		$flist=$f->where("open_id='{$openid}'")->select();
		foreach($flist as $k=>$v){
			$arr[$k]['fzl_f_open_id']=$v['f_open_id'];
			$arr[$k]['fzl_f_price']=$v['f_price'];
			$arr[$k]['fzl_create_at']=$v['created_at'];
			$arr[$k]['fzl_f_score']=$v['f_score'];
			$arr[$k]['content']='好友'.$v['content'];
			$arr[$k]['status']=0;
			$arr1[$k]['zl_f_open_id']=$v['f_open_id'];
			$arr1[$k]['zl_f_price']=$v['f_price'];
			$arr1[$k]['zl_create_at']=$v['created_at'];
			$arr1[$k]['zl_f_score']=$v['f_score'];
			$arr1[$k]['content']=$v['content'].'好友';
			$arr1[$k]['status']=1;

		}
		$z=M('Finance');//查询积分商城积分花销记录
		$jifen=$z->where("open_id='{$openid}'")->select();
		foreach($jifen as $k =>$v){
			if($v['sz_type']==1){
				$jifen[$k]['status']=0;
				$jifen[$k]['money']=$v['money'];
			}
			if($v['sz_type']==2){
				$jifen[$k]['status']=1;
				$jifen[$k]['money']=$v['money'];
			}
		}
		if($jifen==null){
			$jifen=0;
		}
		if($arr==null){
			$arr=0;
		}
		if($arr1==null){
			$arr1=0;
		}
		$zlist['goods_duihuan']=$jifen;
		$zlist['fzl']=$arr;
		$zlist['zl']=$arr1;
		$this->ajaxReturn($zlist);
	}

	//我帮助记录
	public function help(){
        $openid=I('openid');
		$f=M('rob_log');//查询积分助力记录
		$flist=$f->where("f_open_id='{$openid}'")->group('open_id')->select();//帮助的所有人
		//$num=0;
		foreach($flist as $k=>$v){
            $re= M('Member')->where(array('openid' =>$v['open_id']))->find();
            //帮助的第一个人的信息
            //所有人的信息
            $flist[$k]['userInfo']=$re;
            $flist[$k]['count']=$f->where(array("f_open_id"=>$openid,'open_id'=>$v['open_id']))->count();//我作为朋友帮助了这个人几次
           
		}
		$jisuan=$f->where("f_open_id='{$openid}'")->select();//帮助的所有人
		$num=0;
		foreach($jisuan as $kk=>$vv){
             $num=$num+$vv['f_score'];
		}
		$flist1['bangzhu']=$flist;  
		$flist1['num']=$num;
		$this->ajaxReturn($flist1);
	}
	//帮助我的记录
	public function helpme(){
        $openid=I('openid');
		$f=M('rob_log');//查询积分助力记录
		$flist=$f->where("open_id='{$openid}'")->group('f_open_id')->select();//帮助我的记录
		
		foreach($flist as $k=>$v){
            $re= M('member')->where(array('openid' =>$v['f_open_id']))->find();//所有人的信息
            $flist[$k]['userInfo']=$re;
            $flist[$k]['count']=$f->where(array("open_id"=>$openid,"f_open_id"=>$v['f_open_id']))->count();
            //$num=$num+$v['score'];
		}
		if($flist==null){
			$flist=0;
		}
		$jisuan=$f->where("open_id='{$openid}'")->select();//帮助的所有人
		$num=0;
		foreach($jisuan as $kk=>$vv){
             $num=$num+$vv['score'];
		}
		
		$flist1['bangzhu']=$flist;  
		$flist1['num']=$num;
		$this->ajaxReturn($flist1);
	}
	
	
	
	//我的评价 
	public function getPingjiaByOpenid(){
	    $open_id = session('openid');
	    $re = M('Comment c')
	           ->join("yang_member m ON c.open_id=m.openid")
	           ->join("yang_goods g ON c.goods_id=g.goods_id")
	           ->where(array('openid' =>$open_id))->select();
	    foreach ($re as &$v){
	            $price = M('Goods_attr')->where(array('goods_id'=>$v['goods_id'],'is_price_main'=>1))->find();
	            $v['price'] = $price['price'];
	            $v['jifen'] = $price['jifen'];
	            $v['fanjifen'] = $price['fanjifen'];
	    	$v['score'] = intval($v['score']);
	    }
	    $this->ajaxReturn(['status'=>1,'msg'=>'请求成功','list'=>$re],'JSONP');
	}
	//修改手机号
	public function setPhone(){
	    $open_id = session('openid');
	    $data['userPhone'] = I('userPhone');
	    if(!checkMobile($data['userPhone'])){
		    $this->ajaxReturn(['status'=>-1,'msg'=>'手机号码格式不正确'],'JSONP');
		}
	    $re = M('Member')->where(array('openid' =>$open_id))->save($data);
	    $this->ajaxReturn(['status'=>1,'msg'=>'修改成功'],'JSONP');
	}
	//修改真实姓名
	public function setRealName(){
	    $open_id = session('openid');
	    $data["realname"] = I('realname');
	    if(!isChineseName($data["realname"])){
	        $this->ajaxReturn(['status'=>-1,'msg'=>'格式错误'],'JSONP');
	    }
	    $re = M('Member')->where(array('openid' =>$open_id))->save($data);
	    if($re){
	        $this->ajaxReturn(['status'=>1,'msg'=>'修改成功'],'JSONP');
	    }else{
	        $this->ajaxReturn(['status'=>-11,'msg'=>'修改失败'],'JSONP');
	    }
	    
	}
	//查财务日志
	public function getFinance(){
		$type = I('type');
		$open_id = session('openid');
		$list1 = $this->selectFinance($type);
		$user = M('Member')->where(array('openid' =>$open_id))->find();
		if($type == 1){
		    $all_money = $user['money'];
		}
		if($type == 2){
		    $all_money = $user['jifen'];
		}
		$type1 = M('Member')->where(array('openid' =>$open_id))->find();
		$list['type'] = $type1['type'];
		$list['list'] = $list1;
		$list['all_money'] = $all_money;
		$this->ajaxReturn($list,'JSONP');
	}
	//转积分  
	public function changJifen(){
	    $open_id = session('openid');
	    $jifen = I('jifen');
	    $jifencode = I('jifencode');
	    $r = M('Member')->where(array('openid' =>$open_id))->find();
	    if($r['type'] != 1){
	        $this->ajaxReturn(['status'=>-4,'msg'=>'您没有此权限'],'JSONP');
	    }
	    $res = M('Member')->where(array('jifencode'=>$jifencode))->find();
	    if(!$res){
	        $this->ajaxReturn(['status'=>-2,'msg'=>'积分码不存在'],'JSONP');
	    }
	    if($jifen > $r['jifen']){
	        $this->ajaxReturn(['status'=>-3,'msg'=>'积分不足'],'JSONP');
	    }
	    M()->startTrans();
	    
	    $re[] = M('Member')->where(array('openid' =>$open_id))->setDec('jifen',$jifen);
	    $re[] = $this->addFinance(2,2,$jifen,'分配积分');
	    $re[] = M('Member')->where(array('jifencode'=>$jifencode))->setInc('jifen',$jifen);
	    $re[] = $this->addFinance(1,2,$jifen,'分配积分',$res['openid']);
	    if(in_array(false, $re)){
	        M()->rollback();
	        $this->ajaxReturn(['status'=>-1,'msg'=>'系统繁忙，请稍后再试'],'JSONP');
	    }else{
	        M()->commit();
	        $this->ajaxReturn(['status'=>1,'msg'=>'操作成功'],'JSONP');
	    }
	    
	}
	//积分配置
	Public function getJifenConfig(){
		$r = M("Config")->where(array('key'=>'JIFENBILI'))->find();
		$bili = "    1:".$r['value'] ."(金额:积分)";
		$this->ajaxReturn($bili,'JSONP');
	}
}